﻿using SQLServer.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Tenancy.Helper;

namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_OrderList 的摘要说明
    /// </summary>
    public class Hbung_OrderList : IHttpHandler
    {
        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //用户登录的手机号
            string Hbung_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(Hbung_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                Hbung_Phone = DESEncrypt.MD5Decrypt(Regex.Replace(Hbung_Tel, @"\s", "+"));
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(Hbung_Phone) != Regex.Replace(HBsign, @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            //页数
            string CurPage = context.Request.Params["PageIndex"];
            if (string.IsNullOrEmpty(CurPage))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("PageIndex值为空"));
                return;
            }
            int Indexnum = 0;
            if (!string.IsNullOrEmpty(CurPage))
            {
                if (!int.TryParse(CurPage, out Indexnum) || Indexnum < 0)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("PageIndex值不是数字"));
                    return;
                }
            }
            //每页多少数据
            string ageSize = context.Request.Params["PageSize"];
            int Sizenum = 0;
            if (string.IsNullOrEmpty(ageSize))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("pageSize值为空"));
                return;
            }
            if (!string.IsNullOrEmpty(ageSize))
            {
                if (!int.TryParse(ageSize, out Sizenum) || Sizenum < 0)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("pageSize值不是数字"));
                    return;
                }
            }
            //订单的状态 0 1 2 3
            string StrOrder = context.Request.Params["Status"];
            if(string.IsNullOrEmpty(StrOrder))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Status值为空"));
                return;
            }
            else
            {
                if(StrOrder!="0"&&StrOrder!="1"&&StrOrder!="2"&&StrOrder!="3")
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Status格式不对"));
                    return;
                }
            }
            string Models = string.Empty;
         
//            string StrOr = string.Format(@"select  top {2} c.ID,c.OrderNum,c.ProductType,c.PBrands,c.PEquipmentType,c.PMachineModel,c.PMachineNo,c.OrderStatus,
//                   c.ParkProvince,c.ParkCity,c.RentWay,c.RentUnitPrice,b.Tonnage,b.SMUCurr,b.PlatformHeight,b.DriveWay,e.PicPath,DeviceDID
//                    from OnlineOrders c
//                     left join ({0}) e on REPLACE(c.PMachineModel,'/','')=e.PicName
//                     left join (select Tonnage,SMUCurr,PlatformHeight,DriveWay,id from View_DeviceDetail ) b on c.DeviceDID=b.id
//                     where CPhone='{0}' and OrderStatus='{1}'
//                     and OrderNum not in (select top  " + (Indexnum - 1) * Sizenum + @" OrderNum from OnlineOrders where CPhone='{0}' and OrderStatus='{1}' order by  Creationtime desc )
//                   order by Creationtime desc
//
//              ", Hbung_Phone, StrOrder, Sizenum);

            string StrOr = string.Format(@"select  top {2} c.ID,c.OrderNum,c.ProductType,c.PBrands,c.PEquipmentType,c.PMachineModel,c.PMachineNo,c.OrderStatus,
                   c.ParkProvince,c.ParkCity,c.RentWay,c.RentUnitPrice,b.Tonnage,b.SMUCurr,b.PlatformHeight,b.DriveWay,DeviceDID
                    from OnlineOrders c
                    
                     left join (select Tonnage,SMUCurr,PlatformHeight,DriveWay,id from View_DeviceDetail ) b on c.DeviceDID=b.id
                     where CPhone='{0}' and OrderStatus='{1}'
                     and OrderNum not in (select top  " + (Indexnum - 1) * Sizenum + @" OrderNum from OnlineOrders where CPhone='{0}' and OrderStatus='{1}' order by  Creationtime desc )
                   order by Creationtime desc

              ", Hbung_Phone, StrOrder, Sizenum);


            DataTable HB_ComInfo = DbLocalSQL.Query(StrOr).Tables[0];
            string Hb_DateJson = JsonHelper.DataTableToJSON(HB_ComInfo);
            context.Response.Write(GenerClass.SetSucessJsData("查询成功", Hb_DateJson));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}